package com.kaibes.platform.member.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.kaibes.core.base.annotation.Description;
import com.kaibes.web.admin.AdminController;
import com.kaibes.module.user.UserService;
import com.kaibes.platform.member.db.MemberLevelService;
import com.kaibes.platform.member.db.MemberService;
import com.kaibes.platform.member.db.MemberVipService;
import com.kaibes.web.api.ApiLink;

@AdminController
@RequestMapping("/admin/member")
public class AdminMemberController {
    
    @Autowired
    private UserService userService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private MemberLevelService memberLevelService;
    @Autowired
    private MemberVipService memberVipService;
    
    @GetMapping("/info")
    @ApiLink("会员信息管理")
    public String member(Model model, 
            @RequestParam(defaultValue = "25") @Description("每页数量") int size,
            @RequestParam(defaultValue = "1") @Description("页数") int page, 
            @RequestParam(defaultValue = "") @Description("搜索") String search,
            @RequestParam(defaultValue = "0") @Description("会员等级ID") int memberLevelId,
            @RequestParam(defaultValue = "0") @Description("VIP等级ID") int memberVipId) {
        search = search.trim();
        if (!search.isEmpty()) {
            model.addAttribute("userPage", userService.pageNicknameByNicknameLike(page, size, search));
        } else if (memberLevelId != 0) {
            model.addAttribute("memberPage", memberService.pageByLevelId(page, size, memberLevelId));
        } else if (memberVipId != 0) {
            model.addAttribute("memberPage", memberService.pageByVipId(page, size, memberVipId));
        } else {
            model.addAttribute("userPage", userService.pageNickname(page, size));
        }
        model.addAttribute("memberVipList", memberVipService.list());
        return "member-info";
    }

    @GetMapping("/level")
    @ApiLink("等级会员管理")
    public String level(Model model) {
        model.addAttribute("memberLevelList", memberLevelService.list());
        return "member-level";
    }
    
    @GetMapping("/vip")
    @ApiLink("VIP会员管理")
    public String vip(Model model) {
        model.addAttribute("memberVipList", memberVipService.list());
        return "member-vip";
    }
}
